#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# -= project0/msg_handler.py coding by Scott in 2019-06-05 16:34 with IDE:PyCharm =-
import random
import time

import xmltodict

from utils import get_user_info


class Handler:
    # 普通消息
    def __init__(self, appid, m_type, dom):
        self.appid = appid
        self.md = {
            'text': self.text,
            'image': self.image,
            'voice': self.voice,
            'video': self.video,
            'shortvideo': self.shortvideo,
            'location': self.location,
            'link': self.link,
            'event': self.event,
            'subscribe': self.subscribe,
            'unsubscribe': self.unsubscribe,
            'SCAN': self.scan,
            'LOCATION': self.locations,
            'CLICK': self.click,
            'VIEW': self.view,
        }
        self.type = m_type
        self.dom = dom

    def run(self):
        data = self.md[self.type]()
        return data

    def text(self):
        """
        text 文本消息
        :return:
        """
        to_username = self.dom.getElementsByTagName('ToUserName')[0].childNodes[0].data
        from_username = self.dom.getElementsByTagName('FromUserName')[0].childNodes[0].data  # 发送人openID
        msg = self.dom.getElementsByTagName('Content')[0].childNodes[0].data
        create_time = str(int(time.time()))
        if '机器人' in msg:
            content = '我是机器人'
            dict_msg = {
                'xml': {'ToUserName': from_username, 'FromUserName': to_username, 'CreateTime': create_time,
                        'MsgType': 'text',
                        'Content': content}}
            xml = xmltodict.unparse(dict_msg, encoding='utf-8')
            return xml

        msg_pool = ['回复"机器人"有惊喜', '自带智能机器人哟，回复"机器人"试试']
        content = random.choice(msg_pool)
        dict_msg = {
            'xml': {'ToUserName': from_username, 'FromUserName': to_username, 'CreateTime': create_time,
                    'MsgType': 'text',
                    'Content': content}}

        xml = xmltodict.unparse(dict_msg, encoding='utf-8')
        return xml

    def image(self):
        """
        图片消息
        :return:
        """
        return ''

    def voice(self):
        """
        语音消息
        :return:
        """
        return ''

    def video(self):
        """
        视频消息
        :return:
        """
        return ''

    def shortvideo(self):
        """
        小视频消息
        :return:
        """
        return ''

    def location(self):
        """
        地理位置消息
        :return:
        """
        return ''

    def link(self):
        """
        链接消息
        :return:
        """
        return ''

    # 事件推送
    def event(self):
        """
        事件类型
        :return:
        subscribe 关注/取消关注事件
            扫描带参数二维码事件 eventkey
            qrscene_ 用户未关注时，进行关注后的事件推送
        SCAN 扫描带参数二维码事件
        LOCATION 上报地理位置事件
        CLICK 点击菜单拉取消息时的事件推送
        VIEW 点击菜单跳转链接时的事件推送
        """
        events = self.dom.getElementsByTagName('Event')[0].childNodes[0].data
        data = self.md[events]()
        return data

    def subscribe(self):
        to_username = self.dom.getElementsByTagName('ToUserName')[0].childNodes[0].data
        from_username = self.dom.getElementsByTagName('FromUserName')[0].childNodes[0].data
        create_time = self.dom.getElementsByTagName('CreateTime')[0].childNodes[0].data
        open_id = self.dom.getElementsByTagName('FromUserName')[0].childNodes[0].data
        user_info = get_user_info(self.appid, open_id)
        if not user_info:
            return ''
        try:
            EventKey = self.dom.getElementsByTagName('EventKey')[0].childNodes[0].data
        except Exception as e:
            pass
        # if EventKey:
        #     pass
        content = '亲爱的%s，感谢关注！' % user_info["nickname"]
        dict_msg = {
            'xml': {'ToUserName': from_username, 'FromUserName': to_username, 'CreateTime': create_time,
                    'MsgType': 'text',
                    'Content': content}}

        xml = xmltodict.unparse(dict_msg, encoding='utf-8')
        return xml

    def unsubscribe(self):
        return ''

    def scan(self):
        return ''

    def locations(self):
        return ''

    def click(self):
        return ''

    def view(self):
        return ''
